﻿using BLL;
using System;
using System.Web.UI.WebControls;

namespace Thksoft.Module.StatisticalReport
{
    public partial class BankList_DataReport : BaseUI.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var key = Request.QueryString["ID"];
                if (key == "")
                {
                    MessageBox.Show(this, "参数ID传递错误");
                    return;
                }
                Key = Convert.ToDecimal(key);
                BindData();
            }
        }

        public decimal Key
        {
            get
            {
                var text = FORM_KEY.Value;
                if (text == "")
                {
                    return -1;
                }
                return Convert.ToDecimal(text);
            }
            set
            {
                FORM_KEY.Value = value.ToString();
            }
        }

        protected void SORTING_EVENT(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["ORDER"] == null ? null : (string)ViewState["ORDER"];
            OrderDire = ViewState["ORDER_TYPE"] == null ? null : (string)ViewState["ORDER_TYPE"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "DESC")
                    OrderDire = "ASC";
                else
                    OrderDire = "DESC";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["ORDER"] = SortOrder;
            ViewState["ORDER_TYPE"] = OrderDire;
            BindData();
        }

        protected void BindData()
        {
            var id = Request.QueryString["ID"];
            var year = Request.QueryString["YEAR"];
            var cmy = Request.QueryString["CMY"];
            //查询 所有交易记录
            var sql = @"select month,
       nvl(into_amount,0) into_amount,
       decode(money,
              0,
              0,
              round((nvl(into_amount, 0) / nvl(money, 0)) * 100, 2)) into_lv,
       nvl(out_amount,0) out_amount,
       decode(money,
              0,
              0,
              round(nvl(out_amount, 0) / nvl(money, 0) * 100, 2)) out_lv,
       nvl(money,0) money
  from (select TO_CHAR(b.D_TRANDATE, 'mm') TRANDATE,
               sum(decode(b.n_Flag1, 1, b.n_amt)) into_amount,
               sum(decode(b.n_Flag1, 0, b.n_amt)) out_amount
          from yx_customer c, yx_customer_bank cb, YX_BANK_DETAILLIST b
         where cb.c_number = b.c_accno2
           and cb.n_customer_id = c.n_id
           and b.n_company_id = c.n_company_id
           and b.n_company_id = {0}
           and TO_CHAR(b.D_TRANDATE, 'YYYY') = {1}
           and c.n_id = {2}
         group by TO_CHAR(b.D_TRANDATE, 'mm')) aa
  left join (select TO_CHAR(b.D_TRANDATE, 'mm') month, sum(b.n_amt) money
               from yx_customer c, yx_customer_bank cb, YX_BANK_DETAILLIST b
              where cb.c_number = b.c_accno2
                and cb.n_customer_id = c.n_id
                and b.n_company_id = c.n_company_id
                and b.n_company_id = {0}
                and TO_CHAR(b.D_TRANDATE, 'YYYY') = {1}
                and c.n_id = {2}
              group by TO_CHAR(b.D_TRANDATE, 'mm')) bb
    on aa.TRANDATE = bb.month
 order by bb.month";
            sql = string.Format(sql, cmy, year, id);
            var dt = new YX_BANK_LISTBLL().Query(sql);

            if (dt != null && dt.Rows.Count > 0)
            {
                object[] o = new object[dt.Columns.Count];
                o[0] = "月合计";
                o[1] = dt.Compute("Sum(into_amount)", "");
                o[3] = dt.Compute("Sum(out_amount)", "");
                o[5] = dt.Compute("Sum(money)", "");
                dt.Rows.Add(o);
            }
            TABLE.DataSource = dt;
            TABLE.DataBind();

            Label1.Text = year.ToString() + "银行流水详情";
        }

        protected void btnDaoChu_Click(object sender, EventArgs e)
        {
            toExecl1(Panel1, Label1.Text + DateTime.Now.ToShortDateString());
        }
    }
}